﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.UI.Xaml;
using WinTime.Data;
using WinTime.ViewModels;

namespace WinTime.Helpers;

public class EchartsHelper
{
    public static string RadarChart(string title, string legend, string data)
    {

        using var context = new ApplicationDbContext();
        var categoryList = context.CategoryModels.ToList();
        var indicators = "";
        var theme = "dark";
        if (App.MainWindow.Content is FrameworkElement rootElement)
        {            
            if (rootElement.ActualTheme.Equals(ElementTheme.Dark))
            {
                theme = "dark";
            }
            else
            {
                theme = "light";
            }
        }
        categoryList.ForEach(x =>
        {
            indicators += "{name:'" + x.Name + "',max:100},";
        });
        indicators.TrimEnd(',');
        var result = "var dom = document.getElementById('container');\r\nvar myChart = echarts.init(dom, '" + theme + "', {\r\n  renderer: 'canvas',\r\n  useDirtyRect: false,\r\n});\r\nvar app = {};\r\nvar option;\r\noption = {\r\n  title: {\r\n    text: '" +
            title +
            "',\r\n  },\r\n  legend: " + legend +
            ",\r\n  radar: {\r\n    indicator: [" + indicators + "],\r\n  },\r\n  series: [\r\n    {\r\n      name: '',\r\n      type: 'radar',\r\n      " +
            "data: " + data +
            ",\r\n    },\r\n  ],\r\n};\r\nif (option && typeof option === 'object') {\r\n  myChart.setOption(option);\r\n}\r\nwindow.addEventListener('resize', myChart.resize);\r\n";
        return result;
    }
}
